package com.example.hospital.mapper;

import com.example.hospital.domain.entity.Department;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DepartmentMapper {
    @Insert("INSERT INTO department (department_name, description) VALUES (#{departmentName}, #{description})")
    @Options(useGeneratedKeys = true, keyProperty = "departmentId")
    int insert(Department department);

    @Update("UPDATE department SET department_name=#{departmentName}, description=#{description} WHERE department_id=#{departmentId}")
    int update(Department department);

    @Delete("DELETE FROM department WHERE department_id=#{departmentId}")
    int delete(@Param("departmentId") Long departmentId);

    @Select("SELECT * FROM department WHERE department_id=#{departmentId}")
    Department findById(@Param("departmentId") Long departmentId);

    @Select({
            "<script>",
            "SELECT * FROM department",
            "<where>",
            "  <if test=\"departmentName != null and departmentName != ''\"> AND department_name LIKE CONCAT('%', #{departmentName}, '%') </if>",
            "</where>",
            "ORDER BY department_id DESC",
            "LIMIT #{limit} OFFSET #{offset}",
            "</script>"
    })
    List<Department> list(@Param("departmentName") String departmentName, @Param("limit") int limit, @Param("offset") int offset);

    @Select({
            "<script>",
            "SELECT COUNT(1) FROM department",
            "<where>",
            "  <if test=\"departmentName != null and departmentName != ''\"> AND department_name LIKE CONCAT('%', #{departmentName}, '%') </if>",
            "</where>",
            "</script>"
    })
    long count(@Param("departmentName") String departmentName);
}